package com.gzdh.tang.modu.util;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePayRequest;
import com.alipay.api.request.AlipayUserTwostageCommonUseRequest;
import com.alipay.api.response.AlipayTradePayResponse;
import com.alipay.api.response.AlipayUserTwostageCommonUseResponse;
import com.gzdh.tang.config.ali.AliPayUtil;
import com.gzdh.tang.modu.model.dk.JsonsRootBean;

import java.util.HashMap;
import java.util.Map;

/**
 * 支付宝支持类
 */

public class ALiUtils {

    /**
     * 通过授权码获取支付宝的用户ID
     *
     * @param authCode
     */
    public static Map<String, String> getAliUid(String authCode) {
        Map<String, String> res = new HashMap<>();
        String senceNoStr = String.valueOf(System.currentTimeMillis());
        res.put("sence_no", senceNoStr);
        res.put("aliUid", "");
        try {
            AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
                    AliPayUtil.getQrAuthAppid(), AliPayUtil.getQrAuthPrivateKey(),
                    "json", "GBK", AliPayUtil.getQrAuthPublicKey(),
                    "RSA2");
            AlipayUserTwostageCommonUseRequest request = new AlipayUserTwostageCommonUseRequest();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("dynamic_id", authCode);
            jsonObject.put("sence_no", senceNoStr);
            jsonObject.put("scope","auth_paycode_getuid");
            jsonObject.put("pay_pid", AliPayUtil.getQrAuthPid());
            jsonObject.put("pay_check_strategy", 1);
            request.setBizContent(jsonObject.toJSONString());
            AlipayUserTwostageCommonUseResponse response = alipayClient.execute(request);
//            AlipayUserTwostageCommonUseResponse response = alipayClient.execute(request, null, AliPayUtil.getAppAuthToken());
            if (response.isSuccess()) {
                res.put("aliUid", response.getUserId());
                res.put("sence_no", senceNoStr);
                return res;
            } else {
                return res;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

}
